package com.tdt.alarm.controller;

import java.util.Random;

import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.NotificationCompat;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.tdt.alarm.R;
import com.tdt.alarm.model.SettingDBHelper;
import com.tdt.alarm.model.SettingModel;

public class AlarmActionMathSolve extends Activity {
	Button btnDismiss;
	Button btnChangePos;

	Button btnAnswer1;
	Button btnAnswer2;
	Button btnAnswer3;
	Button btnAnswer4;
	Button[] listButtons = new Button[4];
	RatingBar ratingBar;
	CountDownTimer cdt;
	TextView countDownStatus;
	TextView textViewMathQuestion;
	Ringtone r;
	MediaPlayer mp;
	SettingModel setting;
	SettingDBHelper settingDB;
	LinearLayout headLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm_action_math_solve);

		btnDismiss = (Button) findViewById(R.id.btnDismiss);
		btnChangePos = (Button) findViewById(R.id.btnChangePos);
		textViewMathQuestion = (TextView) findViewById(R.id.textViewMathQuestion);
		btnAnswer1 = (Button) findViewById(R.id.btnAnswer1);
		btnAnswer2 = (Button) findViewById(R.id.btnAnswer2);
		btnAnswer3 = (Button) findViewById(R.id.btnAnswer3);
		btnAnswer4 = (Button) findViewById(R.id.btnAnswer4);
		listButtons[0] = btnAnswer1;
		listButtons[1] = btnAnswer2;
		listButtons[2] = btnAnswer3;
		listButtons[3] = btnAnswer4;
		ratingBar = (RatingBar) findViewById(R.id.ratingBar1);
		countDownStatus = (TextView) findViewById(R.id.countDownStatus);

		headLayout = (LinearLayout) findViewById(R.id.linearLayoutHead);

		ratingBar.setEnabled(false);

		changeButtonPosition();

		cdt = new CountDownTimer(50000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				// countDownStatus.setText("Button will run away after "+millisUntilFinished/1000
				// +"s");

			}

			@Override
			public void onFinish() {
				changeButtonPosition();
			}
		};

		cdt.start();
		btnChangePos.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				changeButtonPosition();
			}
		});

		// set visible to test
		btnChangePos.setVisibility(View.INVISIBLE);
		btnDismiss.setVisibility(View.INVISIBLE);

		btnDismiss.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int numStars = (int) (ratingBar.getRating());

				if (numStars < 4) {
					mp = MediaPlayer.create(getApplicationContext(),
							R.drawable.correct_answer);
					mp.start();
					numStars++;
					ratingBar.setRating(numStars);
					changeButtonPosition();
				} else {
					mp = MediaPlayer.create(getApplicationContext(),
							R.drawable.complete_dismiss);
					mp.start();
					Intent intent = new Intent(getApplicationContext(),
							MainActivity.class);
					cdt.cancel();
					r.stop();
					finish();
					startActivity(intent);
				}

			}
		});

		/*
		 * play ringtone
		 */
		settingDB = new SettingDBHelper(getApplicationContext());
		setting = settingDB.getFirstSetting();
		RingtoneManager mRingtoneManager = new RingtoneManager(
				getApplicationContext());
		mRingtoneManager.setType(RingtoneManager.TYPE_RINGTONE);

		Uri settingRingTone = mRingtoneManager.getRingtoneUri(0);

		if (setting != null) {
			settingRingTone = setting.getRingtone();
		}
		r = RingtoneManager.getRingtone(getApplicationContext(), settingRingTone);

		if (!r.isPlaying()) {
			r.play();
		}
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
				getApplicationContext())
				.setSmallIcon(R.drawable.scrubber_control_pressed_holo)
				.setContentTitle("test").setContentText("some text")
				.setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/*
	 * change Dismiss button position
	 */
	private void changeButtonPosition() {
		// random question
		Random random = new Random();
		final int MAX_RANGE = 100;
		int num1 = random.nextInt(MAX_RANGE);
		int num2 = random.nextInt(MAX_RANGE);
		int result = num1 + num2;
		int resultPosition = random.nextInt(4);
		int falseAnswer;

		// set question to textview
		textViewMathQuestion.setText(num1 + " + " + num2 + " = ?");
		// set answers to button
		for (int i = 0; i < 4; i++) {
			if (i == resultPosition) {
				listButtons[i].setText(result + "");
				listButtons[i].setOnClickListener(onRightAnswerListener);
			} else {
				do {
					falseAnswer = random.nextInt(200);
				} while (falseAnswer == result);
				listButtons[i].setText(falseAnswer + "");
				listButtons[i].setOnClickListener(onFalseAnswerListener);
			}
		}
		if(cdt != null){
			cdt.cancel();
			cdt.start();
		}
	}

	/*
	 * Onclick for button which right answer
	 */
	OnClickListener onRightAnswerListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int numStars = (int) (ratingBar.getRating());
			if (numStars < 4) {
				mp = MediaPlayer.create(getApplicationContext(),
						R.drawable.correct_answer);
				mp.start();
				numStars++;
				ratingBar.setRating(numStars);
				changeButtonPosition();
			} else {
				mp = MediaPlayer.create(getApplicationContext(),
						R.drawable.complete_dismiss);
				mp.start();
				Intent intent = new Intent(getApplicationContext(),
						MainActivity.class);
				cdt.cancel();
				r.stop();
				finish();
				startActivity(intent);
			}
		}
	};

	/*
	 * Onclick for button which false answer
	 */
	OnClickListener onFalseAnswerListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			changeButtonPosition();
		}
	};

	// không cho nhấn nút back
	@Override
	public void onBackPressed() {
		Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
	}
}